package server;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class PaymentPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final String Error = "Error";
	public static final String Info = "Info";
	
	public static final double DefaultRate = 0.10;
	public static final double DefaultRebate = 0;
	
	JPanel topPanel;
	JPanel bottomPanel;
	JButton searchB;
	JTextField passT;
	
	ErrorPanel errorPanel;
	JPanel infoPanel;
	
	JLabel userLabel;
	JTextField rateT;
	JLabel subtotalLabel;
	JLabel subtotalLabel2;
	JLabel subtotalBillLabel;
	JLabel rebateBillLabel;
	JTextField discountT;
	JLabel totalLabel;
	JTextField billT;
	
	JRadioButton nochoiceR;
	JRadioButton visaR;
	JRadioButton debitR;
	JRadioButton comptantR;
	
	MethodPanel methodSelected;
	
	JButton paymentButton;
	JButton calculateButton;
	
	String debug = "";
	String pass = "";
	
	public PaymentPanel(){
		setLayout(new BorderLayout());
		
		topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		topPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 3, 0, Color.gray));
		add(topPanel, BorderLayout.NORTH);
		
		JPanel topLeft = new JPanel();
		topLeft.setLayout(new GridLayout(1, 3, 10, 10));
		topPanel.add(topLeft, BorderLayout.WEST);
		
		topLeft.add(new JLabel("Username"));
		topLeft.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 0, new Color(0,0,0,0)));
		passT = new JTextField();
		topLeft.add(passT);
		searchB = new JButton("Search");
		searchB.addActionListener(new SearchAction());
		topLeft.add(searchB);
		
		bottomPanel = new JPanel();
		add(bottomPanel, BorderLayout.CENTER);
		bottomPanel.setLayout(new CardLayout());
		
		
		//---------------------------------
		errorPanel = new ErrorPanel();
		infoPanel = new JPanel();
		bottomPanel.add(errorPanel, Error);
		bottomPanel.add(infoPanel, Info);
		//---------------------------------
		
		infoPanel.setLayout(new BorderLayout());
		
		JPanel northInfo = new JPanel();
		infoPanel.add(northInfo, BorderLayout.NORTH);
		northInfo.setLayout(new BorderLayout());
		
		JPanel wrapPanel = new JPanel();
		wrapPanel.setLayout(new GridLayout(5, 2));
		northInfo.add(wrapPanel, BorderLayout.NORTH);
		
		wrapPanel.setBorder(BorderFactory.createMatteBorder(15, 100, 15, 100, new Color(0,0,0,0)));
		wrapPanel.add(new JLabel("Username : "));
		userLabel = new JLabel();
		wrapPanel.add(userLabel);
		wrapPanel.add(new JLabel("Password :"));
		wrapPanel.add(new JLabel("12345"));
		wrapPanel.add(new JLabel("Billed time :"));
		wrapPanel.add(new JLabel("2h50"));
		wrapPanel.add(new JLabel("Rate ($/min) :"));
		rateT = new JTextField();
		wrapPanel.add(rateT);
		wrapPanel.add(new JLabel("Sub-Total 1 :"));
		subtotalLabel = new JLabel("");
		wrapPanel.add(subtotalLabel);
		
		JPanel crapPanelMiddle = new JPanel();
		crapPanelMiddle.setLayout(new GridLayout(2, 1));
		northInfo.add(crapPanelMiddle, BorderLayout.CENTER);
		crapPanelMiddle.setBorder(BorderFactory.createMatteBorder(0, 10, 15, 10, new Color(0,0,0,0)));
		
		crapPanelMiddle.add(new JLabel("Please enter a comma separated list of Draft Pub bill(s) to apply an additional rebate."));
		billT = new JTextField();
		crapPanelMiddle.add(billT);
		
		JPanel wrapPanel2 = new JPanel();
		wrapPanel2.setLayout(new GridLayout(5, 2));
		northInfo.add(wrapPanel2, BorderLayout.SOUTH);
		
		wrapPanel2.setBorder(BorderFactory.createMatteBorder(0, 100, 0, 100, new Color(0,0,0,0)));
		wrapPanel2.add(new JLabel("Sub-Total 1 :"));
		subtotalLabel2 = new JLabel("");
		wrapPanel2.add(subtotalLabel2);
		wrapPanel2.add(new JLabel("Rebate from bill : "));
		rebateBillLabel = new JLabel("");
		wrapPanel2.add(rebateBillLabel);
		wrapPanel2.add(new JLabel("Sub-Total 2 :"));
		subtotalBillLabel = new JLabel("");
		wrapPanel2.add(subtotalBillLabel);
		wrapPanel2.add(new JLabel("Rebate (% off): "));
		discountT = new JTextField();
		wrapPanel2.add(discountT);
		wrapPanel2.add(new JLabel("Total :"));
		totalLabel = new JLabel("");
		wrapPanel2.add(totalLabel);
		
		rateT.addActionListener(new CalculateAction());
		discountT.addActionListener(new CalculateAction());
		billT.addActionListener(new CalculateAction());
		rateT.addKeyListener(new TextfieldAction());
		discountT.addKeyListener(new TextfieldAction());
		billT.addKeyListener(new TextfieldAction());
		
		JPanel centerInfo = new JPanel();
		infoPanel.add(centerInfo, BorderLayout.CENTER);
		centerInfo.setLayout(new BorderLayout());
		
		JPanel methodPanel = new JPanel();
		methodPanel.setBorder(BorderFactory.createMatteBorder(25, 25, 0, 0, new Color(0,0,0,0)));
		centerInfo.add(methodPanel, BorderLayout.NORTH);
		methodPanel.setLayout(new GridLayout(2, 1, 10, 5));
		methodPanel.add(new JLabel("Chose method of payment :"));
		JPanel groupPanel = new JPanel();
		groupPanel.setLayout(new GridLayout(1, 3, 10, 10));
		methodPanel.add(groupPanel);
		
		nochoiceR = new JRadioButton("No choice");
		visaR = new JRadioButton("Credit card");
		debitR = new JRadioButton("Debit card");
		comptantR = new JRadioButton("Cash");
		
		groupPanel.add(visaR);
		groupPanel.add(debitR);
		groupPanel.add(comptantR);
		
		ButtonGroup group = new ButtonGroup();
		group.add(nochoiceR);
		group.add(visaR);
		group.add(debitR);
		group.add(comptantR);
		
		nochoiceR.setSelected(true);
		
		visaR.addActionListener(new GroupAction());
		debitR.addActionListener(new GroupAction());
		comptantR.addActionListener(new GroupAction());
		
		methodSelected = new MethodPanel();
		centerInfo.add(methodSelected, BorderLayout.CENTER);
		
		//--------------------------------------------
		JPanel southInfo = new JPanel();
		southInfo.setLayout(new BorderLayout());
		infoPanel.add(southInfo, BorderLayout.SOUTH);
		
		JPanel southEastPanel = new JPanel();
		southInfo.add(southEastPanel, BorderLayout.EAST);
		southInfo.setBorder(BorderFactory.createMatteBorder(0, 0, 15, 25, new Color(0,0,0,0)));
		southEastPanel.setLayout(new GridLayout(1, 2, 20, 0));
		
		paymentButton = new JButton("Save payment");
		calculateButton = new JButton("Calculate Total");
		southEastPanel.add(calculateButton);
		southEastPanel.add(paymentButton, BorderLayout.EAST);
		
		calculateButton.addActionListener(new CalculateAction());
		paymentButton.addActionListener(new PaymentAction());
	}
	
	private class PaymentAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(!visaR.isSelected() && !debitR.isSelected() && !comptantR.isSelected()){
				methodSelected.setText("You must select a method of payment");
				methodSelected.setColor(Color.red);
			}else{
				errorPanel.setText("Payment saved.");
				errorPanel.setColor(Color.black);
				CardLayout c1 = (CardLayout) bottomPanel.getLayout();
				c1.show(bottomPanel, Error);
			}
		}
		
	}
	
	private class GroupAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(visaR.isSelected()){
				methodSelected.setText("Paying with credit card...");
				methodSelected.setColor(Color.black);
			}else if(debitR.isSelected()){
				methodSelected.setText("Paying with debit card...");
				methodSelected.setColor(Color.black);
			}else if(comptantR.isSelected()){
				methodSelected.setText("Paying with cash...");
				methodSelected.setColor(Color.black);
			}
		}
		
	}
	
	private class TextfieldAction implements KeyListener{

		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			paymentButton.setEnabled(false);
			if(e.getKeyCode() == KeyEvent.VK_ENTER){
				calculate();
			}
		}

		@Override
		public void keyTyped(KeyEvent e) {
			
		}
		
	}
	
	public void calculate(){
		int min = 2*60 + 50;
		double rate = Double.parseDouble(rateT.getText());
		double total = min*rate;
		subtotalLabel.setText(total+"$");
		subtotalLabel2.setText(total+"$");
		double rebateFromBill = billT.getText().length()*0.10;
		rebateBillLabel.setText(rebateFromBill+"$");
		double discount = Double.parseDouble(discountT.getText());
		double subtotalBill = total-rebateFromBill;
		subtotalBillLabel.setText(subtotalBill+"$");
		double finalTotal  = subtotalBill*(1-discount/100);
		totalLabel.setText(finalTotal+"");
		paymentButton.setEnabled(true);
	}
	
	private class CalculateAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			calculate();
		}
		
	}
	
	private class SearchAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			pass = passT.getText();
			
			if(pass.equals("")){
				errorPanel.setText("You must enter a password.");
				errorPanel.setColor(Color.red);
				CardLayout c1 = (CardLayout) bottomPanel.getLayout();
				c1.show(bottomPanel, Error);
			}else if(pass.equals("99999")){
				errorPanel.setText("The password 99999 doesn't exist.");
				errorPanel.setColor(Color.red);
				CardLayout c1 = (CardLayout) bottomPanel.getLayout();
				c1.show(bottomPanel, Error);
			}else{
				CardLayout c1 = (CardLayout) bottomPanel.getLayout();
				c1.show(bottomPanel, Info);
				userLabel.setText(pass);
				rateT.setText(DefaultRate+"");
				discountT.setText(DefaultRebate+"");
				nochoiceR.setSelected(true);
				methodSelected.setText("");
				calculate();
			}
			
		}
	}
	
	private class MethodPanel extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel methodLabel = new JLabel("", JLabel.CENTER);
		
		public MethodPanel(){
			this.setLayout(null);
			this.add(methodLabel);
			methodLabel.setBounds(0, 15, 500, 40);
			methodLabel.setFont(new Font("Big font", Font.BOLD, 15));
		}
		
		public void setText(String text){
			methodLabel.setText(text);
		}
		
		public void setColor(Color color){
			methodLabel.setForeground(color);
		}
		
	}
	
	private class ErrorPanel extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel errorLabel = new JLabel("", JLabel.CENTER);
		
		public ErrorPanel(){
			this.setLayout(null);
			this.add(errorLabel);
			errorLabel.setBounds(0, 100, 500, 40);
			errorLabel.setForeground(Color.red);
			errorLabel.setFont(new Font("Big font", Font.BOLD, 16));
		}
		
		public void setText(String text){
			errorLabel.setText(text);
		}
		
		public void setColor(Color color){
			errorLabel.setForeground(color);
		}
		
	}
	
	

}
